package ch.bfh.abcmmh.view.frame;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;

import ch.bfh.abcmmh.view.frame.subpanels.GrassPanel;
import ch.bfh.abcmmh.view.frame.subpanels.StreetPanel;
import ch.bfh.abcmmh.view.interfaces.TSSwingObjects;

public class MainFrame extends JFrame implements TSSwingObjects{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private int frameheight, framewidth;
	private LayoutHelper layoutHelper;
	
	public MainFrame(int streetlength){
		this.frameheight = 300;
		this.framewidth = streetlength;
		
		this.setSize(streetlength, frameheight);
		setLocation(putFrameinScreenCenter());
		this.setName("Mainframe");
		this.setTitle("TrafficSim");
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setResizable(false);
		
		this.layoutHelper = new LayoutHelper();
		
		this.setLayout(new BorderLayout());
		this.add(this.layoutHelper);
		
		this.setVisible(true);
	}

	@Override
	public void repaintSubsandMe() {
		layoutHelper.repaintSubsandMe();
		repaint();
	}

	public JPanel getStreet(){
		return this.layoutHelper.getStreet();
	}
	
	private Point putFrameinScreenCenter() {
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		int xFramePos = (int) (dim.getWidth() - this.framewidth) / 2;
		int yFramePos = (int) (dim.getHeight() - this.frameheight) / 2;
		return new Point(xFramePos, yFramePos);
	}
	
	private class LayoutHelper extends JPanel implements TSSwingObjects{

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		
		private GrassPanel gp1, gp2;
		private StreetPanel streetPanel;
		
		private LayoutHelper(){
			this.setLayout(new GridLayout(3, 1));
			this.gp1 = new GrassPanel();
			this.streetPanel = new StreetPanel();
			this.gp2 = new GrassPanel();
			
			this.add(gp1);
			this.add(streetPanel);
			this.add(gp2);
		}

		@Override
		public void repaintSubsandMe() {
			this.gp1.repaintSubsandMe();
			this.streetPanel.repaintSubsandMe();
			this.gp2.repaintSubsandMe();
			this.repaint();
		}
		
		private JPanel getStreet(){
			return this.streetPanel.getStreet();
		}
		
	}

}
